<?php

if (!defined('IN_IA')) {
	exit('Access Denied');
}

class Order_EweiShopV2Page extends PluginMobilePage
{
	public function main()
	{
		global $_W;
		global $_GPC;
		$uniacid = $_W['uniacid'];
		$mendian_id = $_GPC['mendian_id'];
		$message = $_GPC['message'];
		$goods_id = $_GPC['goods_id'];
		if($message == '1'){
			$message = "请选择商品！";
		}
		

		// 用户所属门店信息
		$member = pdo_fetch('SELECT m.uid, im.* FROM ' . tablename('ewei_shop_member') . ' m LEFT JOIN ' . tablename('ewei_shop_identity_member') . ' im ON m.uid = im.shop_user_id WHERE m.uniacid = :uniacid AND m.openid = :openid', array(':openid' => $_W['openid'], ':uniacid' => $_W['uniacid']));
		$set = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_set') . ' WHERE uniacid = '.$_W['uniacid']);
		if($set['is_use'] == 1){
			$belong_mendian = $this->model->get_mendian($member['uid']);
		}


		// 身份等级
		$identity_level = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND status = 1', array(':uniacid' => $_W['uniacid']));
		$setting = pdo_fetch('select * FROM ' . tablename('ewei_shop_identity_setting') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		if($setting['is_use_identity'] == 0){
			include $this->template('identity/register_notice');
			exit;
		}

		// 本人
		$identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity_member') . ' WHERE uniacid = :uniacid AND status = 1 AND end_time > '.time().' AND shop_user_id = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $member['shop_user_id']));

		// 上一级
		$up_identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity_member') . ' WHERE uniacid = :uniacid AND shop_user_id = :shop_user_id', array(':uniacid' => $_W['uniacid'], ':shop_user_id' => $identity['parent_id']));
		


		if(empty($goods_id)){
			// 判断本人身份
			
			if(!empty($identity['identity_id'])){
				foreach ($identity_level as $value) {
					//是否启用显示条件
					if($value['is_use_show_condition']){
						$up_buy_condition = !empty($value['up_buy_condition']) ? unserialize($value['up_buy_condition']) : array();//购买该身份所需的上级身份
						$self_condition = !empty($value['self_condition']) ? unserialize($value['self_condition']) : array();//购买该身份所需的本人身份

						// 是否启用本人购买限制
						if($setting['is_use_self_condition'] == 1 && $value['is_use_self_condition'] == 1){
							// 是否启用上级购买权限
							if($setting['is_use_buy_condition'] == 1 && $value['is_use_buy_condition'] == 1){
								// 本人身份符合同时上级身份符合的商品
								if(in_array($identity['identity_id'], $self_condition) && in_array($up_identity['identity_id'], $up_buy_condition)){
									$goods_id .= $value['buy_goods_id'] . ',';
								}
							} else {
								// 不启用上级购买权限，启用本人购买权限，本人身份符合的商品
								if(in_array($identity['identity_id'], $self_condition)){
									$goods_id .= $value['buy_goods_id'] . ',';
								}
							}
						} else {
							if($setting['is_use_buy_condition'] == 1 && $value['is_use_buy_condition'] == 1){
								// 不启用本人购买权限，启用上级购买权限，上级身份符合的商品
								if(in_array($up_identity['identity_id'], $up_buy_condition)){
									$goods_id .= $value['buy_goods_id'] . ',';
								}
							} else {
								// 不启用本人、上级购买权限，获取所有商品
								$goods_id .= $value['buy_goods_id'] . ',';
								
							}
						}
					} else {
						// 不启用显示条件
						$goods_id .= $value['buy_goods_id'] . ',';
					}
				}
				
			}else{
				
				// 获取所有商品
				foreach ($identity_level as $key => $value) {
					$goods_id .= $value['buy_goods_id'] . ',';
				}
				
			}
		}
		
		$goods_list = pdo_fetchall('SELECT *,id as goodsid FROM ' . tablename('ewei_shop_goods') . ' WHERE uniacid = :uniacid AND id IN ('.trim($goods_id, ',').')', array(':uniacid' => $_W['uniacid']));


		$account = pdo_fetch('SELECT * FROM ' . tablename('uni_account') . ' WHERE uniacid = :uniacid',array(':uniacid' => $_W['uniacid']));

		

		$total = 0;
		foreach ($goods_list as &$g) {
			$g['total'] = $total;
			foreach ($identity_level as $key => $value) {
				if($g['id'] == $value['buy_goods_id']){
				
					$g['is_use_identity_detail'] = $value['is_use_identity_detail'];
					$g['identity_detail'] = html_entity_decode($value['identity_detail']);
					$g['is_use_buy_num_limit'] = $value['is_use_buy_num_limit'];
					if($value['useful_time'] == '36500') {
						$useful_time = '永久';
					} elseif($value['useful_time'] == '365'){
						$useful_time = '一年';
					} elseif($value['useful_time'] == '730'){
						$useful_time = '两年';
					} else {
						$useful_time = $value['useful_time'].'天';
					}
					$g['useful_time'] = $useful_time;
				}
			}
			
			$goodsdata_temp[] = array('goodsid' => $g['id'], 'total' => $g['total'], 'optionid' => $g['optionid'], 'marketprice' => $g['marketprice'], 'merchid' => $g['merchid'], 'cates' => $g['cates'], 'discounttype' => $g['discounttype'], 'isdiscountprice' => $g['isdiscountprice'], 'discountprice' => $g['discountprice'], 'isdiscountunitprice' => $g['isdiscountunitprice'], 'discountunitprice' => $g['discountunitprice'], 'type' => $g['type'], 'intervalfloor' => $g['intervalfloor'], 'intervalprice1' => $g['intervalprice1'], 'intervalnum1' => $g['intervalnum1'], 'intervalprice2' => $g['intervalprice2'], 'intervalnum2' => $g['intervalnum2'], 'intervalprice3' => $g['intervalprice3'], 'intervalnum3' => $g['intervalnum3'], 'wholesaleprice' => $g['wholesaleprice'], 'goodsalltotal' => $g['goodsalltotal']);
		}

		$createInfo = array('id' => 5, 'gdid' => intval($_GPC['gdid']), 'fromcart' => $fromcart, 'addressid' => !empty($address) && !$isverify && !$isvirtual ? $address['id'] : 0, 'storeid' => 0, 'couponcount' => $couponcount, 'coupon_goods' => $goodsdata_temp, 'isvirtual' => $isvirtual, 'isverify' => $isverify, 'isonlyverifygoods' => $isonlyverifygoods, 'goods' => $goods_list, 'merchs' => $merchs, 'orderdiyformid' => $orderdiyformid, 'giftid' => $giftid, 'mustbind' => $mustbind, 'fromquick' => intval($quickid), 'liveid' => intval($liveid));
		
		include $this->template();
	}

	public function sureorder(){
		global $_W;
		global $_GPC;
		$goods_id = $_GPC['id'];
		$total = $_GPC['total'];
		if(empty($goods_id) || empty($total)){
			$url = mobileUrl('identity/order',array('message' => '1'));
			header("location: $url");
			exit;
		}

		$member = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member') . ' WHERE uniacid = :uniacid AND openid = :openid', array(':openid' => $_W['openid'], ':uniacid' => $_W['uniacid']));
		$goods = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_goods') . ' WHERE uniacid = :uniacid AND id = :id', array(':id' => $goods_id, ':uniacid' => $_W['uniacid']));
		$identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND buy_goods_id = :goods_id', array(':uniacid' => $_W['uniacid'], ':goods_id' => $goods_id));
		$mendian = $this->model->get_mendian($member['uid']);

		$data = array(
			'uniacid' 		=> $_W['uniacid'],
			'uid'	  		=> $member['uid'],
			'user_name' 	=> $member['nickname'],
			'openid'		=> $_W['openid'],
			'goods_id'		=> $goods_id,
			'goods_name'	=> $goods['title'],
			'goods_price' 	=> $goods['marketprice'],
			'goods_total'	=> $total,
			'status'		=> 0,
			'identity'		=> $identity['name'],
			'identity_id'	=> $identity['id'],
			'mendian_id'	=> $mendian['id'],
			'mendian_name'	=> $mendian['title'],
			'buy_time'		=> time(),
		);

		pdo_insert('ewei_shop_identity_order', $data);
		$orderid = pdo_insertid();

		session_start();
		$_SESSION['identity_order_id'] = $orderid;

		// $url = "http://mp.test.weihaima.com/app/index.php?i=".$_W['uniacid']."&c=entry&m=ewei_shopv2&do=mobile&r=order.create&id=5&optionid=0&total=1&giftid&liveid=0";
		$url = mobileUrl('identity/create', array('id' => $goods_id, 'total' => $total, 'giftid' => 0,'liveid' => 0, 'optionid' => 0, 'identity_order_id' => $orderid));
		header("location: $url");
	}
	
}